$.Controller.extend('DirectoriesController', {
	/*load: function(params){
		var ps = new MVC.Controller.Params({event: params.event, element: MVC.$E('/'), leave_state: true });
		this.dblclick(ps);
		var height = (window.innerHeight ? window.innerHeight : MVC.$E(document.documentElement).getHeight());
		MVC.$E('FILES').style.height = (height - 32)+'px';
		MVC.$E('files_container').style.height = (height - 0)+'px';
		//var ps = new MVC.Controller.Params({ element: MVC.$E('/') });
		//this.load_directory(ps);
	},*/
	mouseover : function(el){
		if(!el.hasClass('selected')) el.css("background-color",'#E8EEF7' )
	},
	mouseout : function(el){
		if(!el.hasClass('selected')) el.css("background-color",'' )
	},
	click : function(el){
        this.find('div.selected').removeClass('selected')
		el.addClass("selected")
	},
	contextmenu : function(el){
		this.find('div.selected').removeClass('selected')
		el.addClass("selected")
        
		var menu = JITS.rightClickMenu.open( this.render() , params.event);
		params.event.kill();
		return false;
	},
	dblclick : function(params){
		IDE.clear_selection();
		var el = params.element;
		var next = el.next();
		var size = next.get_children().length;
		if( size != 0 ){
			if(params.leave_state )
				return next.style.display == '' || next.style.display == 'block' ? null : Effect.BlindDown(next);
			else{
				return next.style.display == '' || next.style.display == 'block' ?   Effect.BlindUp(next, {duration: size / 30 }) : Effect.BlindDown(next, {duration: size / 30 });
			}
				
		}
		next.style.display = '';
		next.innerHTML = 'Loading ...';
		this.container = next;
		new MVC.Ajax('/ftp/dir?path='+encodeURIComponent( el.id ), 
			{method: 'get', onComplete: this.continue_to('files_loaded') } );
		
	},
	files_loaded : function(response){
		eval('var response_object = '+response.responseText);
		if(! MainController.check(response_object) ) return;
		this.container.innerHTML = '';
		if(response_object.files.length < 100)
			this.container.style.display = 'none';
		if(response_object.files.length <= 2 ) return;
		DirectoryView.update(response_object, true); // uses this for calling load
		if(response_object.files.length < 100){
			var duration = response_object.files.length;
			//Effect.BlindDown(this.container, {duration: response_object.files.length / 30 }); TODO: Replace
		}
		
		//MVC.run_tests();
	},
	/*load_directory : function(params){
		//var element = params.element;
		//this.dropped_on = element;
		//Droppables.add(  element, {hoverclass: 'hover',onDrop: this.continue_to('dropped') } ); //drop into a folder
		//new Draggable(element, {revert:true} );	
	},*/
	dropped : function(dragged){
		var dragged_id = dragged.id;
		var type = dragged.getAttribute('type');
		if(type == 'directory' && MVC.$E(this.dropped_on).descendantOf(dragged.next())){
			alert("You can't put a folder inside itself!"); return;
		}
		
		var from= new MVC.JFile(dragged_id);
		var to_path = (this.dropped_on.id == '/' ? '':  this.dropped_on.id)+ '/'+ from.file_and_extension();
		if(type == 'file')
			new MVC.Ajax('/ftp/rename_file?from_path='+encodeURIComponent(dragged_id)+'&to_path='+encodeURIComponent(to_path), 
				{onSuccess: this.continue_to('file_renamed') , asynchronous:true, evalScripts:false});
		else
			new MVC.Ajax('/ftp/rename_folder?from_path='+encodeURIComponent(dragged_id)+'&to_path='+encodeURIComponent(to_path), 
				{onSuccess: this.continue_to('folder_renamed') , asynchronous:true, evalScripts:false});
		this.dragged = dragged;
		this.dragged.style.color ='Gray';
		this.dragged.innerHTML = 'Moving ...';
	},
	file_renamed : function(response){
		eval('var object = '+response.responseText);
		if(object.response != 'Renamed File'){  alert(object.response);return;}
		var old_file = MVC.$E(object.from_path);
		var old_container = old_file.parentNode;
		MVC.$E(old_file).remove();
		if(old_container.childNodes.length == 0){//for IE
			old_container.style.display = 'none'
		}
		var f= new MVC.JFile(object.to_path);
		editAreaLoader.getRenameFile('EDITAREA_TEXTAREA', object.from_path, object.to_path, f.file_and_extension());
		DirectoryView.update(object);
		var parent = MVC.$E(object.to_path).parentNode;
		new Effect.Highlight( object.to_path, {startcolor: '#C3D9FF', duration: 0.7});
	},
	folder_renamed : function(response){
		eval('var object = '+response.responseText);
		if(object.response != 'Renamed Folder'){  alert(object.response);return;};
		DirectoryView.rename(object);
	},
    dragstart : function(params){
        params.cache_position();
    }
});